﻿using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dot5.Log
{
    internal class NLogger : ILog
    {
        private static void InitLogger()
        {
            var config = new NLog.Config.LoggingConfiguration();
            var logfile = new NLog.Targets.FileTarget("logfile")
            {
                FileName = "${basedir}/log/NLog ${date:format=yyyy-MM-dd}.log",
                Layout = "${longdate} ${level} \n ${message} \n ${exception}",
            };
            config.AddTarget(logfile);
            config.AddRule(LogLevel.Trace, LogLevel.Fatal, logfile);
            NLog.LogManager.Configuration = config;
        }

        public static ILog Log { get; private set; }

        static NLogger()
        {
            InitLogger();
            Log = new NLogger();
        }

        public void Debug(string msg, Exception ex)
        {
            LogManager.GetLogger(this.GetCallingType().FullName).Debug(ex, msg);
        }

        public void Error(string msg, Exception ex)
        {
            LogManager.GetLogger(this.GetCallingType().FullName).Error(ex, msg);
        }

        public void Info(string msg, Exception ex)
        {
            LogManager.GetLogger(this.GetCallingType().FullName).Info(ex, msg);
        }

        public void Warn(string msg, Exception ex)
        {
            LogManager.GetLogger(this.GetCallingType().FullName).Warn(ex, msg);
        }
    }
}
